package com.ruoyi.store.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.store.mapper.WarehouseGoodsMapper;
import com.ruoyi.store.service.IWarehouseGoodsService;
import com.ruoyi.system.api.domain.store.WarehouseGoods;
import com.ruoyi.system.api.dto.WarehouseGoodsDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * 库存信息Service业务层处理
 *
 * @author wl
 * @date 2024-10-25
 */
@Service
@Slf4j
public class WarehouseGoodsServiceImpl implements IWarehouseGoodsService {
    @Autowired
    private WarehouseGoodsMapper warehouseGoodsMapper;

    /**
     * 查询库存信息
     *
     * @param id 库存信息主键
     * @return 库存信息
     */
    @Override
    public AjaxResult selectWarehouseGoodsById(Integer id) {
        WarehouseGoods warehouseGoods = warehouseGoodsMapper.selectWarehouseGoodsById(id);
        log.info("WarehouseGoodsServiceImpl selectWarehouseGoodsById warehouseGoods:{} id:{}", warehouseGoods, id);
        return AjaxResult.success(warehouseGoods);
    }

    @Override
    public BigDecimal getAllPrice() {
        return warehouseGoodsMapper.getAllPrice();
    }

    /**
     * 查询库存信息列表
     *
     * @param warehouseGoodsDTO 库存信息
     * @return 库存信息
     */
    public List selectWarehouseGoodsList(WarehouseGoodsDTO warehouseGoodsDTO) {
        startPage();
        List warehouseGoodsList = new ArrayList<>();
        //是否根据商品分组
        if (warehouseGoodsDTO.getIsGroupByGoods() != null && warehouseGoodsDTO.getIsGroupByGoods()) {
            warehouseGoodsList = warehouseGoodsMapper.selectWarehouseGoodsListGroupByGoods(warehouseGoodsDTO);
        } else {
            warehouseGoodsList = warehouseGoodsMapper.selectWarehouseGoodsList(warehouseGoodsDTO);
        }
        log.info("WarehouseGoodsServiceImpl selectWarehouseGoodsList: {} warehouseGoodsDTO:{}", warehouseGoodsList, warehouseGoodsDTO);
        return warehouseGoodsList;
    }

    public List<WarehouseGoods> takingList(WarehouseGoods warehouseGoods) {
        log.info("WarehouseGoodsServiceImpl takingList params:{}", warehouseGoods);
        List<WarehouseGoods> warehouseGoodsList = warehouseGoodsMapper.takingList(warehouseGoods);
        return warehouseGoodsList;
    }

    /**
     * 新增库存信息
     *
     * @param warehouseGoods 库存信息
     * @return 结果
     */
    @Override
    public AjaxResult insertWarehouseGoods(WarehouseGoods warehouseGoods) {
        //校验商品id是否存在
        if (warehouseGoods.getGoodsId() == null) {
            return AjaxResult.error("商品id不能为空");
        }
        //校验仓库id是否存在
        if (warehouseGoods.getHouseId() == null) {
            return AjaxResult.error("仓库id不能为空");
        }
        //校验库存数量是否为空
        if (warehouseGoods.getStock() == null) {
            warehouseGoods.setStock(0);
        }
        //校验库存数量是否为负数
        if (warehouseGoods.getStock() < 0) {
            return AjaxResult.error("库存数量不能为负数");
        }
        //校验库存预警值是否为空
        if (warehouseGoods.getStockWaring() == null) {
            warehouseGoods.setStockWaring(0);
        }
        //校验库存预警值是否为负数
        if (warehouseGoods.getStockWaring() < 0) {
            return AjaxResult.error("库存预警值不能为负数");
        }
        //校验库存id与商品id组合是否重复
        WarehouseGoods searchWarehouseGoods = warehouseGoodsMapper.selectWarehouseGoodsByGoodsIdAndHouseId(warehouseGoods);
        log.info("WarehouseGoodsServiceImpl addWarehouseGoods searchWarehouseGoods:{}", searchWarehouseGoods);
        if (searchWarehouseGoods != null) {
            warehouseGoods.setId(searchWarehouseGoods.getId());
            warehouseGoods.setStock(searchWarehouseGoods.getStock() + warehouseGoods.getStock());
            AjaxResult rs = updateWarehouseGoods(warehouseGoods);
            return rs;
        }

        //获取当前登录用户信息，设置创建人
        warehouseGoods.setCreateBy(SecurityUtils.getUsername());
        warehouseGoods.setCreateDate(new Date());
        //数据库持久化操作添加仓库信息
        int addNum = warehouseGoodsMapper.insertWarehouseGoods(warehouseGoods);
        log.info("WarehouseGoodsServiceImpl addWarehouseGoods warehouseGoods:{}, addNum:{}", warehouseGoods, addNum);
        if (addNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改库存信息
     *
     * @param warehouseGoods 库存信息
     * @return 结果
     */
    @Override
    public AjaxResult updateWarehouseGoods(WarehouseGoods warehouseGoods) {
        //校验库存id是否存在
        if (warehouseGoods.getId() == null) {
            return AjaxResult.error("库存id不能为空");
        }
        //校验库存数量是否为负数
        if (warehouseGoods.getStock() != null && warehouseGoods.getStock() < 0) {
            return AjaxResult.error("库存数量不能为负数");
        }
        //校验库存预警值是否为负数
        if (warehouseGoods.getStockWaring() != null && warehouseGoods.getStockWaring() < 0) {
            return AjaxResult.error("库存预警值不能为负数");
        }

        //获取当前登录用户信息，设置修改人
        warehouseGoods.setModifyBy(SecurityUtils.getUsername());
        warehouseGoods.setModifyDate(new Date());
        //数据库持久化操作添加仓库信息
        int updNum = warehouseGoodsMapper.updateWarehouseGoods(warehouseGoods);
        log.info("WarehouseGoodsServiceImpl updateWarehouseGoods warehouseGoods:{}, updNum:{}", warehouseGoods, updNum);
        if (updNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据商品id查询库存信息
     * @date 2024/10/28 下午4:41
     * @Param goodsId:
     **/
    public AjaxResult selectWarehouseGoodsByGoodsId(Integer goodsId) {
        if (goodsId == null) {
            return AjaxResult.error("商品id不能为空");
        }
        List<WarehouseGoods> warehouseGoodsList = warehouseGoodsMapper.selectWarehouseGoodsByGoodsId(goodsId);
        return AjaxResult.success(warehouseGoodsList);
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据商品和仓库获取库存
     * @date 2024/11/5 下午4:06
     * @Param goodsId:
     * @Param houseId:
     **/
    public AjaxResult getStockByGoodsIdHouseId(Integer goodsId, Integer houseId) {
        if (goodsId == null || houseId == null) {
            return AjaxResult.error("商品和仓库不能为空");
        }
        WarehouseGoods warehouseGoods = new WarehouseGoods();
        warehouseGoods.setGoodsId(goodsId);
        warehouseGoods.setHouseId(houseId);
        Integer stock = warehouseGoodsMapper.selectStockByGoodsIdHouseId(warehouseGoods);
        if (stock == null) {
            stock = 0;
        }
        log.info("WarehouseGoodsServiceImpl getStockByGoodsIdHouseId goodsId:{}, warehouseId:{}, stock:{}", goodsId, houseId, stock);
        return AjaxResult.success(stock);
    }


    @Override
    public Integer getStockByGoodsId(Integer goodsId) {

        return warehouseGoodsMapper.selectStockByGoodsId(goodsId);
    }

    /**
     * 批量删除库存信息
     *
     * @param ids 需要删除的库存信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteWarehouseGoodsByIds(Integer[] ids) {
        //校验仓库信息id是否存在
        if (ids == null) {
            return AjaxResult.error("500", "库存id不能为空");
        }
        int delNum = warehouseGoodsMapper.deleteWarehouseGoodsByIds(ids);
        log.info("WarehouseGoodsServiceImpl deleteWarehouseGoodsByIds id:{}, delNum:{}", ids, delNum);
        if (delNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }

    /**
     * 删除库存信息信息
     *
     * @param id 库存信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteWarehouseGoodsById(Integer id) {
        //校验仓库信息id是否存在
        if (id == null) {
            return AjaxResult.error("500", "库存id不能为空");
        }
        int delNum = warehouseGoodsMapper.deleteWarehouseGoodsById(id);
        log.info("WarehouseGoodsServiceImpl deleteWarehouseGoodsById id:{}, delNum:{}", id, delNum);
        if (delNum > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败");
    }
}
